package com.imut.hcdp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import cn.hutool.core.lang.hash.Hash;
import com.imut.hcdp.config.exception.ServiceException;
import com.imut.hcdp.domain.HcdpTrain;
import com.imut.hcdp.mapper.HcdpTrainMapper;
import jdk.internal.org.objectweb.asm.Handle;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imut.hcdp.mapper.HcdpTrainSeatMapper;
import com.imut.hcdp.domain.HcdpTrainSeat;
import com.imut.hcdp.service.IHcdpTrainSeatService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


@Service
public class HcdpTrainSeatServiceImpl implements IHcdpTrainSeatService
{
    @Autowired
    private HcdpTrainSeatMapper hcdpTrainSeatMapper;

    @Autowired
    private HcdpTrainMapper hcdpTrainMapper;


    @Override
    public HcdpTrainSeat selectHcdpTrainSeatById(Long id)
    {
        return hcdpTrainSeatMapper.selectHcdpTrainSeatById(id);
    }


    @Override
    public List<HcdpTrainSeat> selectHcdpTrainSeatList(HcdpTrainSeat hcdpTrainSeat)
    {
        return hcdpTrainSeatMapper.selectHcdpTrainSeatList(hcdpTrainSeat);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertHcdpTrainSeat(List<HcdpTrainSeat> hcdpTrainSeat)
    {
        if(CollectionUtils.isEmpty(hcdpTrainSeat)){
            throw new ServiceException("座为信息不可为空！");
        }
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        Long countSeat = 0L;
        Long trainId = null;
        for (int i = 0; i < hcdpTrainSeat.size(); i++) {
            HcdpTrainSeat temp = hcdpTrainSeat.get(i);
            String s = stringStringHashMap.get(temp.getSeattype());
            if(StringUtils.isNotEmpty(s)){
                throw new ServiceException("座位类型名称重复！");
            }
            stringStringHashMap.put(temp.getSeattype(),"none");
            Long seatnum = temp.getSeatnum();
            trainId = temp.getTrainid();
            countSeat += seatnum;
        }
        HcdpTrain hcdpTrain = hcdpTrainMapper.selectHcdpTrainById(trainId);
        if(hcdpTrain == null){
            throw new ServiceException("车辆信息有误！");
        }
        Long totalseat = hcdpTrain.getTotalseat();
        if(!totalseat.equals(countSeat)){
            throw new ServiceException("座位数不符！总座位数：" + totalseat + " 各座位类型总数：" + countSeat);
        }
        int i = hcdpTrainSeatMapper.deleteHcdpTrainSeatByTrainId(trainId);
        for (int j = 0; j < hcdpTrainSeat.size(); j++) {
            HcdpTrainSeat temp = hcdpTrainSeat.get(j);
            temp.setCrttime(new Date());
            hcdpTrainSeatMapper.insertHcdpTrainSeat(temp);
        }
        return  1;
    }


    @Override
    public int updateHcdpTrainSeat(HcdpTrainSeat hcdpTrainSeat)
    {
        return hcdpTrainSeatMapper.updateHcdpTrainSeat(hcdpTrainSeat);
    }


    @Override
    public int deleteHcdpTrainSeatByIds(Long[] ids)
    {
        return hcdpTrainSeatMapper.deleteHcdpTrainSeatByIds(ids);
    }


    @Override
    public int deleteHcdpTrainSeatById(Long id)
    {
        return hcdpTrainSeatMapper.deleteHcdpTrainSeatById(id);
    }
}
